package ming;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Objects;
import java.util.Random;

public class MainFrame extends JFrame implements KeyListener {

    int [][] datas=new int [4][4];
    int x0;
    int y0;
    int step =0; //得分
    int victory=0;

    public MainFrame() {
        initFrame();//初始化窗体
        initData();//数据打乱
        paintView();//绘制界面
        this.addKeyListener(this);//添加键盘监听
        setVisible(true); //设置窗体可见
    }

    public void initFrame(){
        setSize(650, 620); //设置宽高
        setLocationRelativeTo(null); //设置窗体居中
        setAlwaysOnTop(true); //设置窗体置顶
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置关闭模式
        setTitle("幽冥许的Java小游戏"); //设置窗体标题
        setLayout(null); //取消默认布局
        setResizable(false);//锁定界面大小
    }

    public void paintView() {

        //移除掉，界面中所有的内容
        getContentPane().removeAll();



        int[][] vs={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
        to:
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (datas[i][j]!=vs[i][j]){
                    break to;
                }else {
                    victory++;
                }
            }
        }

        if (victory==16){
            JLabel win = new JLabel(new ImageIcon(Objects.requireNonNull(getClass().getResource("/material/win.png"))));
            win.setBounds(185,255, 266, 88);
            getContentPane().add(win);
        }else {
            victory=0;
        }

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel image = new JLabel(new ImageIcon(Objects.requireNonNull(getClass().getResource("/material/" + datas[i][j] + ".png"))));
                image.setBounds(118 + 100 * j, 125 + 100 * i, 95, 95);
                getContentPane().add(image);

            }
        }
        
        JLabel scoreLabel = new JLabel("步数:" + step);
        scoreLabel.setBounds(520, 16, 1000, 25);
        scoreLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        getContentPane().add(scoreLabel);

        JLabel background = new JLabel(new ImageIcon(Objects.requireNonNull(getClass().getResource("/material/background.png"))));
        background.setBounds(90,65, 450, 484);
        getContentPane().add(background);

        JLabel head = new JLabel(new ImageIcon(Objects.requireNonNull(getClass().getResource("/material/head.png"))));
        head.setBounds(0,0, 650, 55);
        getContentPane().add(head);

        //刷新界面的方法
        getContentPane().repaint();
    }

    public void initData(){
        int[]muns={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random random = new Random();
        for (int i = 0; i < muns.length; i++) {
            int index =random.nextInt(muns.length);
            int temp=muns[i];
            muns[i]=muns[index];
            muns[index]=temp;
        }
        for (int i = 0; i < muns.length; i++) {
            datas[i/4][i%4]=muns[i];
            if (muns[i]==0){
                x0=i/4;
                y0=i%4;
            }
        }
    }

    public void moveToTop(){
        datas[x0][y0]=datas[x0+1][y0];
        datas[x0+1][y0]=0;
        x0++;
        paintView();
    }

    public void moveToBottom(){
        datas[x0][y0]=datas[x0-1][y0];
        datas[x0-1][y0]=0;
        x0--;
        paintView();
    }

    public void moveToLeft(){
        datas[x0][y0]=datas[x0][y0+1];
        datas[x0][y0+1]=0;
        y0++;
        paintView();
    }

    public void moveToRight(){
        datas[x0][y0]=datas[x0][y0-1];
        datas[x0][y0-1]=0;
        y0--;
        paintView();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode=e.getKeyCode();
        if (keyCode == 65 ) {       //A:65
            if (y0!=3&&victory!=16){
                step++;
                moveToLeft();//调用左移动方法
            }
        } else if (keyCode == 68 ) {   //D:68
            if (y0!=0&&victory!=16){
                step++;
                moveToRight();//调用右移动方法
            }

        } else if (keyCode == 87) {   //W:87
            if (x0!=3&&victory!=16){
                step++;
                moveToTop();//调用上移动方法
            }

        } else if (keyCode == 83) {   //S:83
            if (x0 != 0&&victory!=16) {
                step++;
                moveToBottom();//调用下移动方法
            }
        }else if (keyCode==32){
            initData();
            step=0;
            victory=0;
            paintView();
        }
    }



    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}
